package com.atbun.eduservice.controller.front;

import com.atbun.commonutils.JwtUtils;
import com.atbun.commonutils.R;
import com.atbun.commonutils.vo.EduCourseOrderVo;
import com.atbun.eduservice.client.OrderClient;
import com.atbun.eduservice.entity.EduCourse;
import com.atbun.eduservice.entity.front.CourseQueryVo;
import com.atbun.eduservice.entity.front.CourseWebVo;
import com.atbun.eduservice.entity.vo.ChapterVo;
import com.atbun.eduservice.service.EduChapterService;
import com.atbun.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Description : 客户端 课程信息
 * @Author : mayp
 * @Date: 2020-05-18 07:52
 */
@RestController
@RequestMapping("/eduservice/coursefront")
public class CourseFrontController {

    @Autowired
    private EduCourseService eduCourseService;
    @Autowired
    private EduChapterService eduChapterService;
    @Autowired
    private OrderClient orderClient;
    /**
     * 分页按条件查询课程信息
     * @param page
     * @param limit
     * @param courseQueryVo
     * @return
     */
    @PostMapping("getCoursePage/{page}/{limit}")
    public R getCoursePage(@PathVariable long page,
                           @PathVariable long limit,
                           @RequestBody CourseQueryVo courseQueryVo){
        Page<EduCourse> params = new Page<>(page,limit);
        Map<String, Object> map = this.eduCourseService.pageListWeb(params,courseQueryVo);
        return R.ok().data(map);
    }

    /**
     * 根据课程id获取课程详情信息
     * @param courseId
     * @return
     */
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId, HttpServletRequest request){
//        根据课程id获取课程，讲师信息
        CourseWebVo courseWebVo = this.eduCourseService.selectInfoWebById(courseId);

//        根据课程id获取章节信息
        List<ChapterVo> chapterVoList = this.eduChapterService.nestedList(courseId);

//        远程调用，判断课程是否被购买
        boolean buyCourse = this.orderClient.isBuyCourse(JwtUtils.getMemberIdByJwtToken(request), courseId);

        return R.ok().data("courseWebVo",courseWebVo).data("chapterVoList",chapterVoList).data("buyCourse",buyCourse);
    }

//    根据课程id获取课程信息（需要被order远程调用）
    @GetMapping("getCourseOrderById/{courseId}")
    public EduCourseOrderVo getCourseOrderById(@PathVariable String courseId){
        EduCourse eduCourse = this.eduCourseService.getById(courseId);
        EduCourseOrderVo eduCourseOrderVo = new EduCourseOrderVo();
        BeanUtils.copyProperties(eduCourse,eduCourseOrderVo);
        return eduCourseOrderVo;
    }


}
